/**
 * Simple regex experiment to create an automatic emoticons by Indolaron
 * Visit: http://www.indolaron.net
 * Update: 23 Oct 2012
 */

$(function() {

        // Append an emoticon bar before comment-form
        if (putEmoAbove) {
                $(putEmoAbove).before('<div class="emoWrap"> :) :( :wa: :D =D ^:D @@, :-d :ya: :&#39;( T_T :p B) :Q 7:( &#92;o/ &#92;m/ ^o^ 7:O x@ (-.-,) :bye:</div>');
        }
        function emo(emo, imgRep, emoKey) {
                $(emoRange).each(function() {
                        $(this).html($(this).html()
                        .replace(/<br>(:|;|=|\^)/g, "<br> $1")
                        .replace(emo, " <img src='" + imgRep + "' class='emo delayLoad' alt='" + emoKey + "' />"));
                });
        }
        emo(/\s:\)+/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/smile.gif", ":)");
  emo(/\s:\(/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/sadanimated.gif", ":(");
        emo(/\s@@,/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/rolleyes.gif", "@@,");
        emo(/\s:ya:/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/yaya.gif", ":ya:");
        emo(/\s:D/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/haha.gif", ":D");
        emo(/\s=D/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/hihi.gif", "=D");
        emo(/\s\^:D/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/abovemehaha.gif", "^:D");
        emo(/\s:'\(/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/cry.gif", ":&#39;(");
        emo(/\s:wa:/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/wawa.gif", ":wa:");
        emo(/\sT_T/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/tears.gif", "T_T");
        emo(/\sB\)/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/cool.gif", "B)");
        emo(/\s:Q/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/smoking.gif", ":Q");
        emo(/\s7:\(/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/conf.gif", "7:(");
        emo(/\s:p/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/wee.gif", ":p");
        emo(/\s7:O/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/angry.gif", "7:O");
        emo(/\s\\o\//ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/applause.gif", "&#92;o/");
        emo(/\s\\m\//ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/metal.gif", "&#92;m/");
        emo(/\s\^o\^/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/angelwhite.gif", "^o^");
        emo(/\sx\@/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/marahbesar.gif", "x@");
        emo(/\s:-d/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/top.gif", ":-d");
        emo(/\s:bye:/ig, "http://reader-download.googlecode.com/svn/trunk/images/emo/bye.gif", ":bye:");
        emo(/\s\(-\.-,\)/g, "http://reader-download.googlecode.com/svn/trunk/images/emo/pumpkin.gif", "(-.-,)");

        // Show alert one times!
        $('.emoWrap').one("click", function() {
                if(emoMessage) {
                        alert(emoMessage);
                }
        });
        // Click to show the code!
        $('.emo').css('cursor', 'pointer').live("click", function(e) {
                $('.emoKey').remove();
                $(this).after('<input class="emoKey" type="text" size="6" value=" ' + this.alt + '" />');
                $('.emoKey').trigger("select");
                e.stopPropagation();
        });
        $(document).on("click", function() {
                $('.emoKey').remove();
        });

});

document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}

function addSmiley(){
  var postBodyClass = (postBodyClass) ? postBodyClass : 'post-body';
  var d = document.getElementsByClassName(postBodyClass);
  for(var i=0;i<d.length;i++){
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\)/g, ' <img src="https://sites.google.com/site/indolaron/icon/smile.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s;-*\)/g, ' <img src="https://sites.google.com/site/indolaron/icon/blink.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\$/g, ' <img src="https://sites.google.com/site/indolaron/icon/oops.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\(/g, ' <img src="https://sites.google.com/site/indolaron/icon/sad.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*D/g, ' <img src="https://sites.google.com/site/indolaron/icon/lol.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*p/g, '<img src="https://sites.google.com/site/indolaron/icon/tongue.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*k/g, ' <img src="https://sites.google.com/site/indolaron/icon/kiss.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\@/g, ' <img src="https://sites.google.com/site/indolaron/icon/angry.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\#/g, ' <img src="https://sites.google.com/site/indolaron/icon/sealed.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*x/g, ' <img src="https://sites.google.com/site/indolaron/icon/oh.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*o/g, ' <img src="https://sites.google.com/site/indolaron/icon/oeye.png" style="border:0; margin:0; padding:0;" id="new" /> ');
    }

var d = document.getElementsByClassName('comment-body-author');
  for(var i=0;i<d.length;i++){
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\)/g, ' <img src="https://sites.google.com/site/indolaron/icon/smile.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s;-*\)/g, ' <img src="https://sites.google.com/site/indolaron/icon/blink.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\$/g, ' <img src="https://sites.google.com/site/indolaron/icon/oops.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\(/g, ' <img src="https://sites.google.com/site/indolaron/icon/sad.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*D/g, ' <img src="https://sites.google.com/site/indolaron/icon/lol.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*p/g, '<img src="https://sites.google.com/site/indolaron/icon/tongue.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*k/g, ' <img src="https://sites.google.com/site/indolaron/icon/kiss.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\@/g, ' <img src="https://sites.google.com/site/indolaron/icon/angry.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\#/g, ' <img src="https://sites.google.com/site/indolaron/icon/sealed.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*x/g, ' <img src="https://sites.google.com/site/indolaron/icon/oh.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*o/g, ' <img src="https://sites.google.com/site/indolaron/icon/oeye.png" style="border:0; margin:0; padding:0;" id="new" /> ');
    }

var d = document.getElementsByClassName('author-comments');
  for(var i=0;i<d.length;i++){
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\)/g, ' <img src="https://sites.google.com/site/indolaron/icon/smile.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s;-*\)/g, ' <img src="https://sites.google.com/site/indolaron/icon/blink.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\$/g, ' <img src="https://sites.google.com/site/indolaron/icon/oops.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\(/g, ' <img src="https://sites.google.com/site/indolaron/icon/sad.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*D/g, ' <img src="https://sites.google.com/site/indolaron/icon/lol.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*p/g, '<img src="https://sites.google.com/site/indolaron/icon/tongue.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*k/g, ' <img src="https://sites.google.com/site/indolaron/icon/kiss.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\@/g, ' <img src="https://sites.google.com/site/indolaron/icon/angry.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\#/g, ' <img src="https://sites.google.com/site/indolaron/icon/sealed.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*x/g, ' <img src="https://sites.google.com/site/indolaron/icon/oh.png" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*o/g, ' <img src="https://sites.google.com/site/indolaron/icon/oeye.png" style="border:0; margin:0; padding:0;" id="new" /> ');
    }

var d = document.getElementsByClassName('widget-content');
  for(var i=0;i<d.length;i++){
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\)/g, ' <img src="https://sites.google.com/site/indolaron/icon/smile.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s;-*\)/g, ' <img src="https://sites.google.com/site/indolaron/icon/blink.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\$/g, ' <img src="https://sites.google.com/site/indolaron/icon/oops.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\(/g, ' <img src="https://sites.google.com/site/indolaron/icon/sad.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*D/g, ' <img src="https://sites.google.com/site/indolaron/icon/lol.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*p/g, '<img src="https://sites.google.com/site/indolaron/icon/tongue.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*k/g, ' <img src="https://sites.google.com/site/indolaron/icon/kiss.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\@/g, ' <img src="https://sites.google.com/site/indolaron/icon/angry.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*\#/g, ' <img src="https://sites.google.com/site/indolaron/icon/sealed.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*x/g, ' <img src="https://sites.google.com/site/indolaron/icon/oh.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
        d[i].innerHTML = d[i].innerHTML.replace(/\s:-*o/g, ' <img src="https://sites.google.com/site/indolaron/icon/oeye.gif" style="border:0; margin:0; padding:0;" id="new" /> ');
    }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(addSmiley);
